package com.uenpay.im.ui.old.activity.pwd;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.TextView;
import com.qiyunxin.android.http.HttpInvokeResultCode;
import com.socks.library.KLog;
import com.uenpay.im.IMApplication;
import com.uenpay.im.R;
import com.uenpay.im.callback.IOnBottomDialogListener;
import com.uenpay.im.ui.old.activity.login.LoginAndRegisterHandle;
import com.uenpay.im.weight.DialogUtility;
import com.uenpay.im.weight.InputView;
import com.uenpay.util.app.base.BaseActivity;
import java.text.MessageFormat;

import butterknife.BindView;
import xyz.royliu.library.AKeyboardManager;

/**
 * @author SL
 * @version 1.0
 * @copyRight http://www.qiyunxin.com
 * @E-mail:
 * @date 创建时间：2016-5-6 下午1:42:29
 * @function 修改支付密码
 */
public class SetPayPwdActivity extends BaseActivity {

	private View loading;
	private AKeyboardManager mKeyBoardManager;
	private InputView inputView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		isFullScreen = true;
		super.onCreate(savedInstanceState);
	}

	@Override
	public View bindView() {
		return null;
	}

	@Override
	public int bindLayout() {
		return R.layout.activity_update_pay_pwd_layout;
	}

	@Override
	public void initView(View view) {

		initView();
		initListener();
	}

	protected void initView() {

		((TextView) findViewById(R.id.title_textview)).setText(R.string.pay_pwd);
		loading = findViewById(R.id.loading);

		inputView = (InputView) findViewById(R.id.inputview);

		mKeyBoardManager = new AKeyboardManager(this);
		mKeyBoardManager.attachTo(inputView, AKeyboardManager.KEYBOARD_TYPE_NUM);
	}

	protected void initListener() {
	/*	final PasswordView pwdView = (PasswordView) findViewById(R.id.password_layout);
		pwdView.setOnFinishInput(false, new OnPasswordInputFinish() {

			@Override
			public void inputFinish() {
				String payPwd = pwdView.getStrPassword();
				if (!TextUtils.isEmpty(payPwd)) {
					setPayPwd(payPwd);
				}
			}

		});*/
		inputView.addTextChangedListener(new TextWatcher() {
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {

			}

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				KLog.d("onTextChanged"+s.toString());
			}

			@Override
			public void afterTextChanged(Editable s) {
                if (s.length()==inputView.getPasswordLength()){
					KLog.d("afterTextChanged"+s.toString());
                   	setPayPwd(s.toString());
                }
			}
		});

		findViewById(R.id.back_layout).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	private void setPayPwd(String payPwd) {
		loading.setVisibility(View.VISIBLE);

		String phoneNum = IMApplication.getUserMobilePhone();
		if (!TextUtils.isEmpty(phoneNum)) {
			getRegisterCaptcha(payPwd, phoneNum);
		}

		// if (haspaypassword == 1) {
		// String phoneNum = IMApplication.getUserMobilePhone();
		// if (!TextUtils.isEmpty(phoneNum)) {
		// getRegisterCaptcha(payPwd, phoneNum);
		// }
		// } else {
		// // 设置支付密码
		// UpdatePersonInfoCommonHandle.setPayPwd(payPwd,
		// new ISetPayPwdResultListener() {
		//
		// @Override
		// public void onPayPwdResult(int status, String msg) {
		// loading.setVisibility(View.GONE);
		// if (!TextUtils.isEmpty(msg)) {
		// IMApplication.showToast(msg);
		// }
		// if (status == HttpInvokeResultCode.RESULT_OK) {
		// finish();
		// }
		// }
		// });
		// }

	}

	private void showDialog(final String payPwd) {
		String send_vercode = MessageFormat.format(getResources().getString(R.string.send_vercode), IMApplication.getUserMobilePhone());
		DialogUtility.showSingleBtnDialog(SetPayPwdActivity.this, send_vercode, R.string.sure, true, new IOnBottomDialogListener() {

			@Override
			public void onClicked() {
				Intent intent = new Intent(SetPayPwdActivity.this, GetPayPwdVercodeActivity.class);
				intent.putExtra("new_pay_pwd", payPwd);
				intent.putExtra("type", 1);
				startActivity(intent);
				finish();
			}
		});
	}

	/**
	 * 设置支付密码，获取验证码
	 *
	 * @param phoneNum
	 */
	private void getRegisterCaptcha(final String payPwd, String phoneNum) {
		LoginAndRegisterHandle loginAndRegisterHandle = new LoginAndRegisterHandle();
		loginAndRegisterHandle.getRegisterCaptcha(phoneNum, "pay", new LoginAndRegisterHandle.IRegisterCaptchaResultListener() {

			@Override
			public void onRegisterCaptchaResult(int status, String msg, String vercode) {
				if (status == HttpInvokeResultCode.RESULT_OK) {
					showDialog(payPwd);
				} else {
					loading.setVisibility(View.GONE);
					if (!TextUtils.isEmpty(msg))
						IMApplication.showToast(msg);
				}
			}
		});

	}

	@Override
	public void onBackPressed() {
		finish();
		super.onBackPressed();
	}
}
